import django_filters
from .models import CommissionRule, CommissionRecord, Invoice, FinancialReport, PaymentTransaction


class CommissionRuleFilter(django_filters.FilterSet):
    commission_type = django_filters.ChoiceFilter(choices=CommissionRule.COMMISSION_TYPES)
    is_active = django_filters.BooleanFilter()
    
    class Meta:
        model = CommissionRule
        fields = ['commission_type', 'is_active']


class CommissionRecordFilter(django_filters.FilterSet):
    status = django_filters.ChoiceFilter(choices=CommissionRecord.STATUS_CHOICES)
    order = django_filters.NumberFilter(field_name='order__id')
    start_date = django_filters.DateFilter(field_name='created_at', lookup_expr='gte')
    end_date = django_filters.DateFilter(field_name='created_at', lookup_expr='lte')
    
    class Meta:
        model = CommissionRecord
        fields = ['status', 'order', 'start_date', 'end_date']


class InvoiceFilter(django_filters.FilterSet):
    status = django_filters.ChoiceFilter(choices=Invoice.STATUS_CHOICES)
    invoice_type = django_filters.ChoiceFilter(choices=Invoice.INVOICE_TYPES)
    order = django_filters.NumberFilter(field_name='order__id')
    start_date = django_filters.DateFilter(field_name='issue_date', lookup_expr='gte')
    end_date = django_filters.DateFilter(field_name='issue_date', lookup_expr='lte')
    due_date = django_filters.DateFilter(field_name='due_date', lookup_expr='lte')
    
    class Meta:
        model = Invoice
        fields = ['status', 'invoice_type', 'order', 'start_date', 'end_date', 'due_date']


class FinancialReportFilter(django_filters.FilterSet):
    report_type = django_filters.ChoiceFilter(choices=FinancialReport.REPORT_TYPES)
    start_date = django_filters.DateFilter(field_name='start_date', lookup_expr='gte')
    end_date = django_filters.DateFilter(field_name='end_date', lookup_expr='lte')
    
    class Meta:
        model = FinancialReport
        fields = ['report_type', 'start_date', 'end_date']


class PaymentTransactionFilter(django_filters.FilterSet):
    status = django_filters.ChoiceFilter(choices=PaymentTransaction.STATUS_CHOICES)
    payment_method = django_filters.ChoiceFilter(choices=PaymentTransaction.PAYMENT_METHODS)
    gateway = django_filters.CharFilter(field_name='gateway', lookup_expr='icontains')
    start_date = django_filters.DateFilter(field_name='created_at', lookup_expr='gte')
    end_date = django_filters.DateFilter(field_name='created_at', lookup_expr='lte')
    
    class Meta:
        model = PaymentTransaction
        fields = ['status', 'payment_method', 'gateway', 'start_date', 'end_date']
